package spring

import (
	"reflect"
	"sync"

	"github.com/go-spring/spring-core/gs"
)

type MyContainer struct {
	lock sync.RWMutex

	Values       []interface{}
	MapValues    map[string]interface{}
	MapExts      map[string][]*PluginExtension
	MapBeanIdDef map[string]*gs.BeanDefinition // java: bean name default use bean id
	MapInterface map[string]reflect.Type
}

func NewMyContainer() *MyContainer {
	return &MyContainer{
		lock:         sync.RWMutex{},
		Values:       make([]interface{}, 0, 30),
		MapValues:    make(map[string]interface{}, 30),
		MapExts:      make(map[string][]*PluginExtension, 30),
		MapBeanIdDef: make(map[string]*gs.BeanDefinition, 30),
		MapInterface: make(map[string]reflect.Type, 30),
	}
}

func (m *MyContainer) Object(i interface{}, beanDef *gs.BeanDefinition, id string, exts []*PluginExtension) {
	m.lock.Lock()
	defer m.lock.Unlock()

	m.Values = append(m.Values, i)
	m.MapValues[id] = i
	m.MapExts[id] = exts
	m.MapBeanIdDef[id] = beanDef
}

func (m *MyContainer) RegisterInterface(id string, t reflect.Type) {
	m.lock.Lock()
	defer m.lock.Unlock()

	m.MapInterface[id] = t
}

func (m *MyContainer) GetTypeByInterfaceName(id string) reflect.Type {
	return m.MapInterface[id]
}

func (m *MyContainer) GetTypeByBeanName(id string) *gs.BeanDefinition {
	return m.MapBeanIdDef[id]
}

func (m *MyContainer) GetValueByBeanName(id string) interface{} {
	return m.MapValues[id]
}
